from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.urls import reverse

class ArticleColumn(models.Model):
    title = models.CharField(max_length=100,blank=True)
    created = models.DateTimeField(default = timezone.now)

    def __str__(self):
        return self.title

class ArticlePost(models.Model):
    #文章作者
    author = models.ForeignKey(User,on_delete=models.CASCADE)
    #文章标题
    title = models.CharField(max_length=100)
    #文章栏目 一对多外键
    column = models.ForeignKey(
        ArticleColumn,
        null = True,
        blank=True,
        on_delete = models.CASCADE,
        related_name='article'
    )
    #文章正文
    body=models.TextField()
    #创建时间
    created=models.DateTimeField(default=timezone.now)
    #更新时间
    updated = models.DateTimeField(auto_now=True)
    #浏览量
    total_views = models.PositiveIntegerField(default=0)

    
    class Meta:
        ordering = ('-created',)
        
    def __str__(self):
        return self.title;
        
    def get_absolute_url(self):
        return reverse('article:article_detail',args=[self.id])